
importScripts('http://cdn.staticfile.org/spark-md5/3.0.0/spark-md5.min.js');
self.onmessage = function (event) {
    const { chunks } = event.data
    let spark = new SparkMD5.ArrayBuffer()
    let progress = 0
    let count = 0
    const loadNext = index => {
        const reader = new FileReader()
        reader.readAsArrayBuffer(chunks[index].file)
        reader.onload = e => {
            count ++
            spark.append(e.target.result)
            if(count == chunks.length) {
                self.postMessage({
                    type: "0",
                    progress: 100,
                    hash: spark.end()
                })
                self.close();
            } else {
                progress = (count * 100 / chunks.length).toFixed(2)
                self.postMessage({
                    type: "1", progress
                })
                loadNext(count)
            }
        }
    }
    loadNext(count)
}